<?php
header("Content-Type: text/html; charset=iso-8859-1");
class membre{
	var $fields;

	function membre($id,$field = 'id'){
		$fields = describe('membres');
		$this->commissions = array();
		$this->equipes = array();
		$this->entraineurs = array();
		$this->responsables = array();
		$this->marquages = array();
		$this->arbitrages = array();

		$q = "select * from membres where $field = '$id'";
		$r = ExecRequete($q);
		if(!mysql_num_rows($r)){
			$this->id = 0;
			foreach($fields as $field => $type){
				$this->$field = "";
			}
			$this->create_comptabilite();

		}
		else{
			$s = LigneSuivante($r);
			foreach($fields as $field => $type){
				$this->$field = $s->$field;
			}
			$prenom = ucwords($prenom);
			$nom = ucwords($nom);
			$q = "select equipe_id, nom from membre_equipes left join equipes on equipe_id = equipes.id where membre_id = $this->id";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)) $this->equipes[$s->equipe_id] = $s->nom;
			$q = "select equipe_id, nom from entraineurs, equipes  where equipe_id = equipes.id and membre_id = $this->id";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$this->entraineurs[$s->equipe_id] = $s->nom;
			}
			$equipe = new equipe($this->id,'responsable');
			if($equipe->id) $this->responsables[$equipe->id] = $equipe->nom;
			$this->marquages = array();
			$q = "select no from matchs where marqueur1 = '$this->id'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)) $this->marquages[] = $s->no;
			$q = "select no from arbitrages where arbitre1 = '$this->id' or arbitre2 = '$this->id'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)) $this->arbitrages[] = $s->no;
			$this->nb_arbitrages = count($this->arbitrages);
			$q = "select commissions.id as id,fonction, commissions.nom as nom from commissions inner join membre_commissions on commissions.id = commission_id where membre_id = '$this->id'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)) $this->commissions[$s->id] = $s->fonction;
			$this->nb_marquages = "";
			if(count($this->arbitrages)) $this->nb_marquages .= "A, ";
			if(count($this->commissions)) $this->nb_marquages .= "C, ";
			if(array_key_exists("M1",$this->equipes)) $this->nb_marquages .= "M1, ";
			$this->nb_marquages .= count($this->marquages);

			$this->comptabilite = array();
			$q = "select * from comptabilite where membre_id = '$this->id'";
			$r = ExecRequete($q);
			if($s = LigneSuivante($r)){
				$fields_compta = describe('comptabilite');
				foreach($fields_compta as $field => $type){
					$this->comptabilite[$field] = $s->$field;
				}
			}
			else $this->create_comptabilite();
			$tels = array('tel','mobile','prof');
			foreach($tels as $tel) $this->$tel = format_tel($this->$tel);
		}
	}

	function photo($size=100){
		$src = (file_exists("photos/$this->id.jpeg") && ($this->site || check_cookie())) ? "$this->id" : (($this->titre == 'Madame') ? "femme" : "homme");
		return "<div class='shadow'><img id = 'photo_$this->id' class = 'photo' src = 'photos/$src.jpeg' alt = '' height = '$size' width = '$size' /><div class='topleft'></div><div class='topright'></div><div class='bottomleft'></div><div class='bottomright'></div></div>";

	}

	function agenda(){
		if($this->id){
			#### prochain match ######
			$prochains_matchs = "";
			foreach($this->equipes as $equipe_id => $equipe){
				$next_match = new match('next','', $equipe);
				if($next_match->no){
					$localite = ($salle = new salle($next_match->salle)) ? $salle->localite : "??";
					$prochains_matchs .= "<p style = 'font-size: 8pt; line-height: 9pt'><span class = 'bold'>Prochain match:</span><br />".(format_date($next_match->date,"d.m"))." ".(format_heure($next_match->heure))." $localite<br />- $next_match->adversaire -</p>\n";
				}
			}

			### prochain coaching #####
			foreach($this->entraineurs as $equipe_id => $equipe){
				$next_match = new match('next','', $equipe);
				if($next_match->no){
					$localite = ($salle = new salle($next_match->salle)) ? $salle->localite : "??";
					$prochains_matchs .= "<p style = 'font-size: 8pt; line-height: 9pt'><span class = 'bold'>Prochain coaching:</span><br />".(format_date($next_match->date,"d.m"))." ".(format_heure($next_match->heure))." $localite<br />- $next_match->adversaire -</p>\n";
				}
			}
			### prochain marquage ######
			$q = "select no from matchs where (marqueur1 = '$this->id' or marqueur2 = '$this->id') and date >= curdate() order by date asc limit 1";
			$r = ExecRequete($q);
			if($s = LigneSuivante($r)){
				$next_match = new match($s->no);
				if($next_match->no){
					$localite = ($salle = new salle($next_match->salle)) ? $salle->nom : "??";
					$prochains_matchs .= "<p style = 'font-size: 8pt; line-height: 9pt'><span class = 'bold'>Prochain marquage:</span><br />".(format_date($next_match->date,"d.m"))." ".(format_heure($next_match->heure))." $localite</p>\n";
				}
			}
			### prochain arbitrage #####
			$q = "select id from arbitrages where (arbitre1 = '$this->id' or arbitre2 = '$this->id') and date >= curdate() order by date asc limit 1";
			$r = ExecRequete($q);
			if($s = LigneSuivante($r)){
				$next_match = new arbitrage($s->id,"id");
				if($next_match->id){
					$localite = ($salle = new salle($next_match->salle)) ? $salle->localite : "??";
					$prochains_matchs .= "<p style = 'font-size: 8pt; line-height: 9pt'><span class = 'bold'>Prochain arbitrage:</span><br />".(format_date($next_match->date,"d.m"))." ".(format_heure($next_match->heure))." $localite</p>\n";
				}
			}

			if(!$prochains_matchs) $prochains_matchs = "<p class = 'center italic fade'>pas de match</p>\n";
			return $prochains_matchs;
		}
	}

	function matchs(){
		$matchs = array();
		foreach($this->equipes as $id_equipe => $nom_equipe){
			$equipe = new equipe($id_equipe);
			$matchs = array_merge($matchs,$equipe->matchs());
		}
		foreach($this->entraineurs as $equipe_id => $equipe_nom){
			$equipe = new equipe($equipe_id);
			$matchs = array_merge($matchs,$equipe->matchs());
		}
		foreach($this->marquages as $no_match){
			$match = new match($no_match);
			$matchs[] = $match;
		}
		return $matchs;
	}

	function dates(){
		$dates = array();
		$matchs = array();
		foreach($this->equipes as $id_equipe => $nom_equipe){
			$equipe = new equipe($id_equipe);
			$matchs = array_merge($matchs,$equipe->matchs());
		}
		foreach($this->entraineurs as $equipe_id => $equipe_nom){
			$equipe = new equipe($equipe_id);
			$matchs = array_merge($matchs,$equipe->matchs());
		}
		$matchs = array_merge($matchs,$this->marquages);
		foreach($matchs as $match){
			$date = format_date($match->date);
			$heure = format_heure($match->heure);
			$dates[$date."<>".$heure] = $match->salle;
		}
		return $dates;
	}

	function calendrier(){
		$matchs = array();
		$ok_edit = ($this->id == check_cookie()) ? 1 : 0;
		$table = new table();
		$table->filter = 1;
		$table->order = 'date';
		$fields = array("no","cat.","equipe","date","heure","salle","local","visiteur","r&eacute;sultat");
		foreach($fields as $field){
			$$field = new table_field($field);
			if($field == 'no') $$field->hidden = 1;
			elseif($field == 'cat.') $$field->styleclass = 'center';
			elseif($field == "r&eacute;sultat") $$field->styleclass = 'nowrap';
			$table->fields[] = $$field;
		}
		$resultat->styleclass = 'center';
		$id->hidden = 1;
#		$table->styleclass = 'bold_hover';
		$table->filter = 1;
		foreach($this->equipes as $id_equipe => $nom_equipe){
			$equipe = new equipe($id_equipe);
			$matchs = array_merge($matchs,$equipe->matchs());
		}
		foreach($matchs as $match){
			$row = new table_row();
			$row->onclick = "";
			foreach($fields as $field){
				if($field == 'cat.') $echo = 'J';
				elseif($field == 'r&eacute;sultat'){
					if(!$match->resultat  && $ok_edit == 1 && $match->date <= date("Y-m-d")){
						$echo = "<select name = '$match->no' onchange = \"if(this.options[this.selectedIndex].value) update_resultat('$match->no',this.options[this.selectedIndex].value,'membre')\">";
						$echo .= "<option value = ''>score...</option>";
						$tab_scores = (((strpos($match->equipe,'M16')!==FALSE)||(strpos($match->equipe,'M19G')!==FALSE))) ? array("2-0","2-1","1-2","0-2") : array("3-0","3-1","3-2","2-3","1-3","0-3");
						foreach($tab_scores as $score) $echo .= "<option value = '$score'>$score</option>";
						$echo .= "</select><span id = 'ok_$match->no'></span>";
					}
					else $echo = $match->format_resultat();
				}
				elseif($field == 'salle'){
					$salle = new salle($match->salle);
					$echo = $salle->lien();
				}
				else $echo = $match->$field;
				$row->cols[$field] = $echo;
			}
			if($match->date < date('Y-m-d') && strpos($match->resultat,"-")) $row->hidden = 1;
			$table->rows[] = $row;
		}
		foreach($this->marquages as $marquage){
			$match = new match($marquage);
			$row = new table_row();
			$row->onclick = "";
			foreach($fields as $field){
				if($field == 'cat.') $echo = 'M';
				elseif($field == 'r&eacute;sultat') $echo = "marquage";
				elseif($field == 'salle'){
					$salle = new salle($match->salle);
					$echo = $salle->lien();
				}
				else $echo = $match->$field;
				$row->cols[$field] = $echo;
			}
			if($match->date < date('Y-m-d')) $row->hidden = 1;
			$table->rows[] = $row;
		}
		foreach($this->arbitrages as $arbitrage){
			$match = new arbitrage($arbitrage);
			$row = new table_row();
			$row->onclick = "";
			foreach($fields as $field){
				if($field == 'cat.') $echo = 'A';
				elseif($field == 'r&eacute;sultat') $echo = "arbitrage";
				elseif($field == 'salle'){
					$salle = new salle($match->salle);
					$echo = $salle->lien();
				}
				else $echo = $match->$field;
				$row->cols[$field] = $echo;
			}
			if($match->date < date('Y-m-d')) $row->hidden = 1;
			$table->rows[] = $row;
		}

		foreach($this->entraineurs as $equipe_id => $equipe_nom){
			$equipe = new equipe($equipe_id);
			$matchs_coach = $equipe->matchs();
			foreach($matchs_coach as $match){
				$row = new table_row();
				$row->onclick = "";
				foreach($fields as $field){
					if($field == 'cat.') $echo = 'C';
					elseif($field == 'r&eacute;sultat'){
						if(!$match->resultat  && $ok_edit == 1 && $match->date <= date("Y-m-d")){
							$echo = "<select name = '$match->no' onchange = \"if(this.options[this.selectedIndex].value){update_resultat('$match->no',this.options[this.selectedIndex].value,'membre');}\">";
							// $echo = "<select name = '$match->no' onchange = \"alert(this.options[this.selectedIndex].value)\">";
							$echo .= "<option value = ''>score...</option>";
							$tab_scores = (((strpos($match->equipe,'M16')!==FALSE)||(strpos($match->equipe,'M19G')!==FALSE))) ? array("2-0","2-1","1-2","0-2") : array("3-0","3-1","3-2","2-3","1-3","0-3");
							foreach($tab_scores as $score) $echo .= "<option value = '$score'>$score</option>";
							$echo .= "</select><span id = 'ok_$match->no'></span>";
						}
						else $echo = $match->format_resultat();
					}
					elseif($field == 'salle'){
						$salle = new salle($match->salle);
						$echo = $salle->lien();
					}
					else $echo = $match->$field;
					$row->cols[$field] = $echo;
				}
				if($match->date < date('Y-m-d')  && strpos($match->resultat,"-") ) $row->hidden = 1;
				$table->rows[] = $row;
			}
		}
		echo "<div id = 'table_date' class = 'center' style = 'text-align: right; font-size:7pt;margin-bottom: -18px; margin-right: 22px;'><a href = 'javascript:table_date(0,\"$table->tablename\")'>Afficher tous les matchs</a></div>\n";
		echo "<form action = '' >\n";
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();
		echo "</form>\n";
		echo "<p class = 'center fade'>cat&eacute;gories : <span class = 'bold'>J</span> : joueur, <span class = 'bold'>C</span> : coaching, <span class = 'bold'>M</span> : marquage, <span class = 'bold'>A</span> : arbitrage,</p>\n";

	}

function print_resume($box = 0, $marquage = 0, $arbitrage = 0){
	$jours = array("lu" => "lundi",
					"ma" => "mardi",
					"me" => "mercredi",
					"je" => "jeudi",
					"ve" => "vendredi",
					"sa" => "samedi",
					"di" => "dimanche");

	$titre = CapitalizeLastName(strtolower($this->prenom))." ".CapitalizeLastName(strtolower($this->nom))."<span class  ='float_right'>".(implode(', ',$this->equipes))."</span><!---->";
	if((check_cookie() && $this->site) || check_cookie()){
		$right =  "$this->adresse<br />$this->npa&nbsp;$this->localite<br/>\n";
		if($this->email) $right .= "<span class = 'center'>".(print_email($this->email))."</span><br />";
		$right .= "$this->tel<br />$this->mobile";
		$right .= "<p class = 'center' style = 'padding-bottom: 0; margin-bottom: 0;'><button type = 'button' onclick = \"self.location='membre.php?id=$this->id&amp;action=complet'\" >donn&eacute;es compl&egrave;tes</button></p>\n";
		if(!$this->site && !check_cookie()) $right .= "<p class = 'fade'>donn&eacute;es prot&eacute;g&eacute;es</p>";
	}
	elseif($this->site && !check_cookie()) $right = "$this->tel<br />$this->mobile<br />".(print_email($this->email));
	else $right = "<p class = 'fade'>donn&eacute;es prot&eacute;g&eacute;es</p>";
	$left = $this->photo();
	$content = leftCol($left);
	$content .=  rightCol($right);
	$content .=  "<br class = 'clear' />";
	if((check_cookie() && $this->site) || check_cookie()){
		$content .=  "<div style = 'position: absolute; top: 25px; left: 200px; text-align: right; heigth: 23px; width: 29px'><a href = 'vcard.php?id=$this->id'><img src = 'images/icon_vcard.png' alt = 'vcard' title = 'vcard' height = '23' width = '29' /></a></div>\n";
	}
	if($marquage){
		$match = new match($marquage);
		$print_swap = "";
		for($i = 1; $i < 3; $i++){
			$marqueur = ($match->{"marqueur".$i}) ? new membre($match->{"marqueur".$i}) : 0;
			if($marqueur){
				foreach($this->marquages as $match_no){
					$ok = 1;
					$swap_match = new match($match_no);
					if($swap_match->date >= date('Y-m-d') && $swap_match->no != $match->no){
						foreach($marqueur->matchs() as $tmp_match){
							if($tmp_match->date == $swap_match->date && ($tmp_match->salle != $swap_match->salle || in_array($swap_match->equipe,$marqueur->equipes))) $ok = 0;
						}
						if($ok){
							$q = "select jour, salle, heure, membre_equipes.equipe_id as equipe_id from membre_equipes left join entrainements on membre_equipes.equipe_id = entrainements.equipe_id where membre_id = '$marqueur->id'";
							$r = ExecRequete($q);
							while($s = LigneSuivante($r)){
								if($swap_match->jour == $s->jour && $swap_match->salle != $s->salle) $ok = 0;
								elseif($swap_match->jour == $s->jour && $swap_match->salle != $s->salle && !in_array($swap_match->equipe,$marqueur->equipes)){
									$ok = 1;
								}
							}
						}
						if($ok){
							$print_swap .=  "<p>$marqueur->prenom $marqueur->nom  pourrait remplacer $this->prenom pour son marquage du ".($jours[$swap_match->jour])." ".(format_date($swap_match->date))." &agrave; ".(format_heure($swap_match->heure))."</p>";
						}
					}
				}
			}
		}
		if($print_swap){
			$content .= "<h2>Remplacement du marquage du $match->jour ".(format_date($match->date))."</h2>";
			$content .= $print_swap;
		}
	}
	if($box){
		if($box != 2) echo "<div style = 'margin-left: 60px; height: 150px' id = 'membre_details'>\n";
		box("<div id = 'nom'>$titre</div>","<div id = 'details'>$content</div>",500,1);
		if($box != 2) echo "</div>\n";
	}
	else echo $titre.$content;

}

	function print_detail($edit=0){

		$titres = array(
			"id" => "id",
			"titre" => "titre",
			"nom" => "nom",
			"prenom" => "pr&eacute;nom",
			"adresse" => "adresse",
			"npa" => "NPA",
			"localite" => "localit&eacute;",
			"commune" => "commune",
			"email" => "email",
			"tel" => "t&eacute;l. priv&eacute;",
			"prof" => "t&eacute;l. prof.",
			"mobile" => "t&eacute;l. mobile",
			"fax" => "fax",
			"ddn" => "date de naissance",
			"nationalite" => "nationalit&eacute;",
			"marqueur" => "marqueur",
			"arbitre" => "arbitre",
			"arbitre_junior" => "arbitre junior",
			"nolicence" => "No de licence",
			"maj" => "date mise &agrave; jour",
			"admission" => "date admission",
			"demission" => "date d&eacute;mission",
			"site" => "ces donn&eacute;es sont visibles sur le site",
			"lettre_info" => "abonn&eacute; &agrave; la lettre d'info",
			"valide" => "valide",
			"fictif" => "fictif",
			"note" => "note"
		);

		$no_print = array("id","site","valide","lettre_info","fictif");
		$fields = describe('membres');
		$vcard = ($this->id) ? "<span class = 'float_right'><pre id = 'nom_card' style = 'visibility:hidden; display: none;padding-right: 1em;vertical-align: top;font-size: 9pt;'>fichier vCard</pre><a href = 'vcard.php?id=$this->id'><img src = 'images/icon_vcard.png' alt = 'vcard' title = 'vcard' height = '23' width = '29'  onmouseover = \"show_el('nom_card')\" onmouseout = \"hide_el('nom_card')\"/></a></span>" : "";
		echo "<h2>Contact$vcard</h2>\n";
		$onsubmit = (!$this->id) ? "onsubmit = 'return form_membre_check(this)'" : "";
		$action = (check_admin('commission_technique')) ? "membre_admin.php" : "inscriptions.php";
		echo "<form action = '$action' method = 'post' enctype = 'multipart/form-data' name = 'myform' $onsubmit>\n";
		$img = "<div style = 'margin-right: 25px;margin-left: 50px'>".($this->photo())."</div><div style = 'margin-right: 20px; position: relative;margin-top: 120px;padding-left: 0; text-align: left;'>";
		if($edit){
			$img .= "<p>D&eacute;couper d'une photo d&#39;&eacute;quipe: <select name = 'equipe' onchange = \"if(this.options[this.selectedIndex].value) {return !window.open('crop_photo_admin.php?id=$this->id&equipe='+this.options[this.selectedIndex].value,'_blank','width=1000,height=1000,status=1,resizable=1,alwaysRaised=0,scrollbars=1');}\">\n";
			$img .= "<option value = ''>choix...</option>\n";
			if($this->id){
				foreach($this->equipes as $equipe){
					$img .= "<option value = '$equipe'>$equipe</option>\n";
				}
			}
			else{
				$q = "select nom from equipes";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					$img .= "<option value = '$s->nom'>$s->nom</option>\n";
				}
			}
			$img .= "</select></p>\n";
			$img .=  "<div id='iframe'>\n";
			$img .=  "    <iframe src='photo_upload.php?id=$this->id' frameborder='' scrolling='0'></iframe>\n";
			$img .=  "</div>\n";

#			$img .= "<p>A partir d'un fichier : <input type = 'file' size = '5' name = 'photo' onchange = 'document.myform.submit()' /><input type = 'hidden' name = 'MAX_FILE_SIZE' value = '99999' /></p>\n";
		}
		$img .= "<h3 style = 'text-align:left'>Equipes</h3>\n";
		if($edit && check_admin('commission_technique')){
			$img .= "<p class = 'center bold' style = 'line-height: 1.5em;width: 240px;' id = 'nom_complet_equipe'>&nbsp;</p>\n";
			$img .= "<ul style = 'text-align: left; list-style: none; width: 240px;text-indent: 0; margin-left: 0; padding-left: 0'>\n";
			$q = "select id, nom, annee_max,nom_complet from equipes";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				if(($this->titre == 'Madame' && strstr($s->nom,"F") !== FALSE) || ($this->titre == 'Monsieur' && strstr($s->nom,"F") === FALSE || !$this->id || $s->nom == 'M14') ){
					if(date('Y',strtotime($this->ddn)) >= $s->annee_max || empty($s->annee_max)){
						$checked = (array_key_exists($s->id,$this->equipes)) ? "checked = 'checked'" : "";
						$class = (array_key_exists($s->id,$this->equipes)) ? "bold" : "fade";
						$onclick = ($this->id) ? "onclick = \"myclass = (this.checked) ? 'bold' : 'fade';setclass('equipe_$s->id',myclass);if(this.checked) add_team($this->id,$s->id); else del_team($this->id,$s->id);\"" : "onclick = \"myclass = (this.checked) ? 'bold' : 'fade';setclass('equipe_$s->id',myclass);\"";
						$img .= "<li style = 'text-align: left; float: left; width: 80px; list-style: none;' class = '$class' id = 'equipe_$s->id' onmouseover = 'writediv(\"$s->nom_complet\",\"nom_complet_equipe\")' onmouseout = 'writediv(\"&nbsp;\",\"nom_complet_equipe\")'><input type = 'checkbox' name = 'equipe_$s->id' value = '1' $checked $onclick/>$s->nom</li>\n";
					}
				}
			}
			$img .= "</ul><br class = 'clear' />\n";
		}
		else{
			foreach($this->equipes as $equipe){
				$eq = new equipe($equipe,'nom');

				$img .= "<p style = 'text-align:left; margin-left: 25px;'>Joueur : <span class = 'bold'><a href = 'equipes.php?id=$eq->id'>$equipe</a></span></p>\n";
			}
			if(count($this->entraineurs)){
				foreach($this->entraineurs as $equipe_id => $equipe_nom){
					$img .= "<p style = 'text-align:left; margin-left: 25px;'>Entra&icirc;neur : <span class ='bold'><a href = 'equipes.php?id=$equipe_id'>".$equipe_nom."</a></span></p>\n";
				}
			}
			if(count($this->responsables)){
				foreach($this->responsables as $equipe_id => $equipe_nom){
					$img .= "<p style = 'text-align:left; margin-left: 25px;'>Resp. d'&eacute;quipe : <span class ='bold'><a href = 'equipes.php?id=$equipe_id'>".$equipe_nom."</a></span></p>\n";
				}
			}
			if(count($this->commissions)){
				$img .= "<h3 style = 'text-align:left'>Administratif</h3>\n";
				foreach($this->commissions as $comm_id => $fonction ){
					$commission = new commission($comm_id);
					$img .= "<p style = 'text-align:left; margin-left: 25px;'>".$commission->nom." : <span class = 'bold'>".$fonction."</span></p>\n";
				}
			}
		}
		$img .= "</div>\n";
		$content = "";
		foreach($fields as $nom => $type){
			if((!in_array($nom,$no_print) || $edit) && ($nom != 'valide' || check_admin('commission_technique')) && ($nom != 'fictif' || check_admin('commission_technique')) && ($nom != 'note' || check_admin('commission_technique'))){

				if($nom == 'id' || $nom == 'maj' || $nom == 'admission' || $nom == 'demission'){
					$value = ($type == 'date') ? format_date($this->$nom) : $this->$nom;
					if(!$this->id && $nom == 'id') $value = 'nouveau';
					elseif(!$this->id){
						if($nom != 'demission'){
							$value = date("d.m.Y");
							$this->$nom = date("Y-m-d");
						}
					}
					$data = $value."<input type = 'hidden' name = '$nom' value = '".$this->$nom."' />";
				}
				elseif($nom == 'titre'){
					if($edit){
						$onchange = ($this->id) ? "onchange = \"update_db('membres',$this->id,'$nom',this.options[this.selectedIndex].value)\"" : "";
						$data = "<select name  = '$nom' $onchange>\n";
						if($this->$nom == 'Monsieur') $data .= "<option value = 'Madame'>Madame</option><option value = 'Monsieur' selected = 'selected'>Monsieur</option>\n";
						else $data .= "<option value = 'Madame' selected = 'selected'>Madame</option><option value = 'Monsieur' >Monsieur</option>\n";
						$data .= "</select><span id = 'ok_$nom'></span>\n";
					}
					else $data = $this->$nom;
				}
				elseif($type == 'int(1)'){
					if($edit && (strpos($nom,'arbitre') === FALSE||check_admin('commission_technique'))
					 	&& (strpos($nom,'marqueur') === FALSE||check_admin('commission_technique'))){
						$checked = ($this->$nom) ? "checked = 'checked'" : "";
						$onclick = ($this->id) ? " onclick = \"var myvalue = (this.checked) ? 1 : 0;update_db('membres',$this->id,'$nom',myvalue)\"" : "";
						$data = "<input type = 'checkbox' name = '$nom' value = '1' $checked $onclick/><span id = 'ok_$nom'></span>\n";
					}
					else{
						$data = ($this->$nom == 1) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
					}
				}
				elseif ($type == 'date'){
					if($edit){
						if(preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/",$this->$nom,$regs) || !$this->id){
							$day = (count($regs)>1) ? $regs[3] : "jj";
							$month = (count($regs)>1) ? $regs[2] : "mm";
							$year = (count($regs)>1) ? $regs[1] : "aaaa";
							$data = "";
							$onblur = ($this->id) ? "onblur = \"update_db('membres',$this->id,'$nom',document.myform.".$nom."_annee.value+'-'+document.myform.".$nom."_mois.value+'-'+document.myform.".$nom."_jour.value)\"" : "onselect = 'if(document.myform.".$nom."_jour.value == \"jj\") document.myform.".$nom."_jour.value = \"\";if(document.myform.".$nom."_annee.value == \"aaaa\") document.myform.".$nom."_annee.value = \"\";if(document.myform.".$nom."_mois.value == \"mm\") document.myform.".$nom."_mois.value = \"\";'";
							$data .= "<input class = 'center' type = 'text' size = '2' maxlength = '2' name = '".$nom."_jour' value = '$day'  $onblur/>.";
							$data .= "<input class = 'center' type = 'text' size = '2' maxlength = '2' name = '".$nom."_mois' value = '$month' $onblur/>.";
							$data .= "<input class = 'center' type = 'text' size = '4' maxlength = '4' name = '".$nom."_annee' value = '$year' $onblur/><span id = 'ok_$nom'></span>";
						}
					}
					else{
						$data = format_date($this->$nom);
					}
				}
				else{
					if($edit){
						$size = ($nom == 'npa') ? 4 : 20;
						$onblur = ($this->id) ? "onblur = \"update_db('membres',$this->id,'$nom',document.myform.".$nom.".value)\"" : "";
						$data = "<input type = 'text' name = '$nom' value = '".$this->$nom."' size = '$size' $onblur/><span id = 'ok_$nom'></span>";
					}
					else $data =($nom == 'email')? print_email($this->email) : $this->$nom;
				}
				if($data || $edit){
					$content .= leftCol("<span class = 'fade'>$titres[$nom] : </span>",40);
					$content .= rightCol($data,57);
				}
			}
		}
		// if($edit){
		// 	$content .= leftCol("<button type = 'cancel' name = 'annuler' >annuler</button>",40);
		// 	$content .= rightCol("<button type = 'submit' name = 'submit' value = '1'>modifier</button></form>",57);
		// }

		if($this->id) $content .= "</form>\n";
		echo leftCol($content,62);
		echo rightCol($img,36);
		echo "<br class = 'clear'/>";
	}

	function print_admin(){
		if(check_admin()){
			echo "<h2>Administration</h2>\n";
			$q =  "select id, description as nom from commissions";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				echo leftCol("<span class = 'bold'>".(str_replace("_"," ",$s->nom)).":</span>",49);
				if($this->id){
					if(array_key_exists($s->id,$this->commissions)){
						if($this->commissions[$s->id]){
							$value =  utf8_decode($this->commissions[$s->id]);
							$value .= "&nbsp;<a href = \"javascript:update_membre_admin($this->id,$s->id,'".$this->commissions[$s->id]."')\"<img src = 'images/icon_delete.png' alt = 'supprimer' width = '14' height = '14' /></a>";
						}
						else{
							$value = "<span class = 'fade'>aucune fonction</span>";
							$value .= "&nbsp;<a href = \"javascript:update_membre_admin($this->id,$s->id,'new')\"><img src = 'images/icon_new.png' alt = 'ajouter' width = '14' height = '14' /></a>";
						}
					}
					else{
						$value = "<span class = 'fade'>aucune fonction</span>";
						$value .= "&nbsp;<a href = \"javascript:update_membre_admin($this->id,$s->id,'new')\"><img src = 'images/icon_new.png' alt = 'ajouter' width = '14' height = '14' /></a>";
					}
					$value = "<span id = 'commission_$s->id'>".$value."</span>";
				}
				else{
					$value = "<input type = 'text' name = 'commissions_$s->nom' size = '20' />";
				}
				echo rightCol($value,49);
			}
			$action = ($this->id) ? "photo" : "add_new";
			if(!$this->id) echo "<br class = 'clear' /><p class = 'center'><input type = 'hidden' name = 'action' value = '$action' /><button type = 'submit'>enregistrer</button></p></form>\n";
		}
		else{
			if(!$this->id) echo "<br class = 'clear' /><p class = 'center'><input type = 'hidden' name = 'step' value = '3' /><button type = 'submit'>enregistrer</button></p></form>\n";
		}
	}

	function insert(){
		$fields = describe("membres");
		$q = "select id from membres where prenom = '$this->prenom' and nom = '$this->nom'";
		$r = ExecRequete($q);
		if(!mysql_num_rows($r)){
			$q = "insert into membres values(";
			foreach($fields as $field => $type){
				$q .= (isset($this->$field)) ? "'".(addslashes($this->$field))."', " : "'', ";
			}
			$q = substr($q,0,-2);
			$q .= ")";
			if(ExecRequete($q)){
				$ok = 1;
				$this->id = mysql_insert_id();
				foreach($this->equipes as $id_equipe => $nom_equipe){
					$q = "insert into membre_equipes (membre_id,equipe_id) values ($this->id,$id_equipe)";
					$ok = (ExecRequete($q)) ? 1 : 0;
				}
				if(check_admin()){
					foreach($this->commissions as $id_com => $nom_com){
						if($id_com){
							$q = "select * from membre_commissions where membre_id = $this->id and commission_id = $id_com";
							$r = ExecRequete($q);
							if(mysql_num_rows($r) == 0){
								$q = "insert into membre_commissions (membre_id,commission_id) values ($this->id,$id_com)";
								$ok = (ExecRequete($q)) ? 1 : 0;
							}
							$ok = 1;
						}
					}
				}
				$message = ($ok) ? "Membre enregistr&eacute; avec succ&egrave;s" : "D&eacute;sol&eacute;, une erreur s&#x27;est produite. Le membre n&#x27;a pas &eacute;t&eacute; enregistr&eacute;";
				message($message);
			}
			if(!empty($_SESSION['tmp_id'])){
				if(file_exists("photos/".$_SESSION['tmp_id'].".jpeg")) rename("photos/".$_SESSION['tmp_id'].".jpeg", "photos/".$this->id.".jpeg");
			}
			$this->pdf(0);
			return $ok;
		}
		else message("le membre $this->prenom $this->nom est d&eacute;j&agrave; enregistr&eacute;");
		return 0;
	}

	function pdf($output = 1){
		$titres = array(
			"id" => "id",
			"titre" => "titre*",
			"nom" => "nom*",
			"prenom" => "pr&eacute;nom*",
			"adresse" => "adresse*",
			"npa" => "NPA*",
			"localite" => "localit&eacute;*",
			"commune" => "commune",
			"email" => "email",
			"tel" => "t&eacute;l. priv&eacute;*",
			"prof" => "t&eacute;l. prof.",
			"mobile" => "t&eacute;l. mobile*",
			"fax" => "fax",
			"ddn" => "date de naissance*",
			"nationalite" => "nationalit&eacute;*",
			"marqueur" => "marqueur",
			"arbitre" => "arbitre",
			"arbitre_junior" => "arbitre junior",
			"nolicence" => "No de licence",
			"maj" => "date mise &agrave; jour",
			"admission" => "date admission",
			"demission" => "date d&eacute;mission",
			"site" => "ces donn&eacute;es sont visibles sur le site",
			"lettre_info" => "abonn&eacute; &agrave; la lettre d'info",
			"note" => "note"
		);
		$_SESSION['tmp_id'] = session_id();
		$fields = describe("membres");
		$pdf=new FPDF();
		$pdf->SetFont('Arial','B',16);
		$pdf->AddPage();
	    $pdf->Cell(0,20,html_entity_decode("Donn&eacute;es personnelles"),0,0,'C');
	    $pdf->Ln();
		$pdf->SetFont('Arial','',12);
		$urls = array();
		foreach($fields as $field => $type)
	    {
			if($field != 'valide' && $field != 'demission' && $field != 'fictif'){
				$value = "";
				if($type == 'int(1)') $value = ($this->$field) ? "oui" : "non";
				elseif($type == "date") $value = (ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})",$this->$field,$regs)) ? $regs[3].".".$regs[2].".".$regs[1] : $this->$field;
				else $value = $this->$field;
				$urls[] =  $field."=".urlencode(utf8_encode($value));
				// 		        $pdf->Cell(100,8,html_entity_decode($titres[$field])." : ",0,0,'R');
				// $pdf->SetFont('Arial','',12);
				// if(!$value) $value = "..............................................";
				// 		        $pdf->Cell(100,8,$value,0,0,'L');
				// 		        $pdf->Ln();
			}
			$equipes = "";
	    }
		foreach($this->equipes as $id_eq => $nom_eq){
			$equipes .= $nom_eq.", ";
		}
		$equipes = (strlen($equipes)) ? substr($equipes,0,-2) : $equipes;
		$urls[] = "equipes=$equipes";
		$urls[] = "tmp_id=".$_SESSION['tmp_id'];
		// $pdf->SetFont('Arial','B',12);
		//         $pdf->Cell(100,8,html_entity_decode("&eacute;quipes")." : ",0,0,'R');
		// $pdf->SetFont('Arial','',12);
		//         $pdf->Cell(100,8,$equipes,0,0,'L');
		//         $pdf->Ln();
		// $pdf->SetFont('Arial','B',12);
		//         $pdf->Cell(100,8,html_entity_decode("Remarques")." : ",0,0,'R');
		// $pdf->SetFont('Arial','',12);
		// $value = "...........................................................................";
		//         $pdf->Cell(100,8,$value,0,0,'L');
		//         $pdf->Ln();
		// $pdf->Output("/homez.95/vbcll/www/tmp/".$_SESSION['tmp_id'].".pdf","F");
		system("wget --user ".SSH_USER." --password ".SSH_PASSWORD." -O /homez.95/vbcll/www/tmp/".$_SESSION['tmp_id'].".pdf 'http://ludwig-sun1.unil.ch/~rliechti/vbcll/vbcll_merge_pdf.php?".(implode("&",$urls))."'");
		if($output){
			header('Content-type: application/pdf');
			header('Content-Disposition: attachment; filename="inscription_vbcll_'.$this->prenom.'_'.$this->nom.'.pdf"');
			readfile("/homez.95/vbcll/www/tmp/".$_SESSION['tmp_id'].".pdf");
		}
		else return 1;
		// unlink("/homez.95/vbcll/www/tmp/".$_SESSION['tmp_id'].".pdf");
	}

	function create_comptabilite(){
		if(!isset($membre->comptabilite) && $this->id){
			$q = "insert into comptabilite (membre_id) values ($this->id)";
			ExecRequete($q);
		}
	}

	function presences(){
		$presences = array();
		foreach($this->matchs() as $match){
			$q = "select id from membre_matchs where no_match = '$match->no' and membre_id = '$this->id'";
			$r = ExecRequete($q);
			if(!mysql_num_rows($r)) $presences[] = $match->no;
		}
		return $presences;
	}

	function disponibilite_marquage($match){
		if(array_search(format_date($match->date),$this->dates())) return "<span class = 'non'>NF</span>";
		if(array_search($match->equipe,$this->equipes)) return "<span class = 'non'>NF</span>";
		$salles = array();
		$jours = array();
		foreach($this->equipes as $eq_id => $eq_nom){
			$q = "select * from entrainements where equipe_id = '$eq_id' and `match` = 0";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$salles[$s->id] = $s->salle;
				$jours[$s->id] = $s->jour;
			}
		}
		if($entr_id = array_search($match->jour,$jours)){
			if($match->salle == $salles[$entr_id]) return "<span class = 'oui'>TF</span>";
			else return "<span class = 'non'>NF</span>";
		}
		else return "Fav.";
	}

	function vcard(){
		$v = new vCard();
		if($this->tel) $v->setPhoneNumber(str_replace(' ','',$this->tel), "PREF;HOME;VOICE");
		if($this->mobile)	$v->setPhoneNumber(str_replace(' ','',$this->mobile), "CELL");
		if($this->prof) $v->setPhoneNumber(str_replace(' ','',$this->prof), "WORK");
		if(file_exists("photos/$this->id.jpeg")){
			$handle = fopen("photos/$this->id.jpeg",'rb');
			$photo = fread($handle,filesize("photos/$this->id.jpeg"));
			fclose($handle);
			$v->setPhoto("JPEG",$photo);
		}
		$v->setName(CapitalizeLastName(strtolower($this->nom)), CapitalizeLastName(strtolower($this->prenom)),"","");
		$v->setBirthday("$this->ddn");
		$v->setAddress("", "", "$this->adresse", "$this->localite", "", "$this->npa", "");
		if($this->email) $v->setEmail("$this->email");
		if($this->equipes) $v->setNote("Equipe(s): ".implode(", ",$this->equipes));
		$output = $v->getVCard();
		return $output;
	}
}

?>
